package com.binguo.wxapi.controller;


import com.binguo.common.exception.ServiceException;
import com.binguo.helper.FaceHelperV3;
import com.binguo.model.model.Customer;
import com.binguo.wxapi.form.*;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.wxapi.service.WXCustomerService;
import com.binguo.wxapi.service.WxFaceService;
import com.binguo.wxapi.vo.FaceVO;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.binguo.common.exception.ErrorCode.CUSTOMER_NOT_EXIST;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Api(tags = "小程序: 人脸上传")
@RestController
@RequestMapping
public class WxFaceController {

    private static Logger logger = LoggerFactory.getLogger(WxFaceController.class);

    @Resource
    WxFaceService wxFaceService;

    @Resource
    WXCustomerService wxCustomerService;



    static String AccessKey = "2v6frbFEfnfI5xHe5sBP_O6ZDnwDbSiu_zYa_zfF";
    static String SecretKey = "ypc84sVpvQxB98CAsFqH-pK_qkMtot5dwWPFe1Z_";

/*


    @GetMapping(value = "/api/wxapp/qiniu/token", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取七牛上传token")
    public Result getQiniuToken() {
        Auth auth = Auth.create(AccessKey, SecretKey);
        String token = auth.uploadToken("yunpeng");
        return ResultGenerator.genSuccessResult(token);
    }

*/




    @PostMapping(value = "/api/wxapp/face/create", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建人脸", notes = "通过CreateCardForm")
    public Result create(@RequestBody @Valid CreateFaceForm form) {
        Customer customer = wxCustomerService.getById(form.getCustomerId());
        if (customer == null) {
            throw new ServiceException(CUSTOMER_NOT_EXIST);
        }
        wxFaceService.addFaceNew(customer, form.getFaceUrl());

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping(value = "/api/wxapp/face/cmsCreate", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建人脸", notes = "通过CreateCardForm")
    public Result cmsCreate(@RequestBody @Valid CreateCmsFaceForm form) {
        Customer customer = wxCustomerService.getById(form.getCustomerId());
        if (customer == null) {
            throw new ServiceException(CUSTOMER_NOT_EXIST);
        }
        wxFaceService.addCmsFace(customer, form.getFaceUrl(),form.getShopId());

        return ResultGenerator.genSuccessResult();
    }




    @PostMapping(value = "/api/wxapp/face/list", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取人脸列表", notes = "通过FaceListForm")
    public Result faceList(@RequestBody @Valid FaceListForm form) {
        List<FaceVO> faceVOS = wxFaceService.getFaceList(form.getCustomerId());
        return ResultGenerator.genSuccessResult(faceVOS);
    }



    @PostMapping(value = "/api/wxapp/face/del", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除人脸", notes = "通过FaceListForm")
    public Result delFace(@RequestBody @Valid DelFaceForm form) {
        logger.info("delFace: {}" , form);
        List<FaceVO> faceVOS = wxFaceService.getFaceList(form.getCustomerId());
        if (faceVOS.size() == 1) {
            throw new ServiceException("人脸数量必须大于一张");
        }
        wxFaceService.delFace(form.getCustomerId(), form.getFaceId());

        logger.info("WxFaceController.delFace");
        faceVOS = wxFaceService.getFaceList(form.getCustomerId());
        return ResultGenerator.genSuccessResult(faceVOS);
    }

    @PostMapping(value = "/api/wxapp/cmsFace/del", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除人脸", notes = "通过FaceListForm")
    public Result delCmsFace(@RequestBody @Valid cmsFaceForm form) {

        wxFaceService.deletCmsFace(form.getCustomerId(),form.getShopId());

        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/wxapp/face/detectFace", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "检测人脸", notes = "通过CreateCardForm")
    public Result detectFace(@RequestBody @Valid DetectFaceForm form) {
        Integer scorce = FaceHelperV3.detectFace(0,form.getFaceUrl());
        return ResultGenerator.genSuccessResult(scorce);
    }

    @PostMapping(value = "/api/wxapp/face/detectFaceBase64", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "检测人脸", notes = "通过CreateCardForm")
    public Result detectFaceBase64(@RequestBody @Valid DetectFaceForm form) {
        Integer scorce = FaceHelperV3.detectFaceBase64(form.getFaceUrl());
        return ResultGenerator.genSuccessResult(scorce);
    }

    @PostMapping(value = "/api/wxapp/face/cms/addDeviceMsg", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "管理平台审核用户成功添加消息", notes = "添加消息")
    public Result addDeviceMsg(@RequestBody @Valid cmsFaceForm form) {
        wxFaceService.addCmsDeviceMsg(form.getCustomerId(),form.getShopId());
        return ResultGenerator.genSuccessResult();
    }

}
